//
//  KAReadFile.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/18.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAReadFile.h"

@implementation KAReadFile

+ (KAReadFile *)sharedInstance{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

-(void)createBtnPlistFile
{
    NSArray *incomeArr = @[
  @{@"imageName":@"type_btn_income_01.png",@"titleName":@"一般"},
  @{@"imageName":@"type_btn_income_02.png",@"titleName":@"工资"},
  @{@"imageName":@"type_btn_income_03.png",@"titleName":@"红包"},
  @{@"imageName":@"type_btn_income_04.png",@"titleName":@"兼职"},
  @{@"imageName":@"type_btn_income_05.png",@"titleName":@"奖金"},
  @{@"imageName":@"type_btn_income_06.png",@"titleName":@"投资"},
  @{@"imageName":@"type_btn_income_07.png",@"titleName":@"报销"}];
    [self writeToFile:KAIncomePlistPath withData:incomeArr];
    NSArray *outlayArr = @[ @{@"imageName":@"type_btn_outlay_01.png",@"titleName":@"一般"}, @{@"imageName":@"type_btn_outlay_15.png",@"titleName":@"美食"}, @{@"imageName":@"type_btn_outlay_03.png",@"titleName":@"医疗"}, @{@"imageName":@"type_btn_outlay_04.png",@"titleName":@"旅行"}, @{@"imageName":@"type_btn_outlay_16.png",@"titleName":@"娱乐"}, @{@"imageName":@"type_btn_outlay_06.png",@"titleName":@"学习"}, @{@"imageName":@"type_btn_outlay_07.png",@"titleName":@"酒水"}, @{@"imageName":@"type_btn_outlay_08.png",@"titleName":@"数码"}, @{@"imageName":@"type_btn_outlay_09.png",@"titleName":@"服饰"}, @{@"imageName":@"type_btn_outlay_10.png",@"titleName":@"日用品"}, @{@"imageName":@"type_btn_outlay_11.png",@"titleName":@"用餐"}, @{@"imageName":@"type_btn_outlay_12.png",@"titleName":@"零食"}];
    [self writeToFile:KAOutlayPlistPath withData:outlayArr];
}

-(void) writeToFile:(NSString *)pathFile withData:(id)data
{
    BOOL isWrite = [data writeToFile:pathFile atomically:YES];
    if (isWrite) {
        //[SVProgressHUD showSuccessWithStatus:@"数据加载完成."];
    } else {
        [SVProgressHUD showErrorWithStatus:@"数据加载失败."];
    }
}

-(void)writeIncomePlistWithData:(NSDictionary *)dict
{
    NSMutableArray *marr = [[NSMutableArray alloc]initWithContentsOfFile:KAIncomePlistPath];
    [marr addObject:dict];
    [self writeToFile:KAIncomePlistPath withData:marr];
}

-(NSMutableArray *)readPlistFile:(NSString *)plistPath
{
    NSArray *arr = [NSArray arrayWithContentsOfFile:plistPath];
    NSMutableArray *marr = [NSMutableArray array];
    for (NSDictionary *dict in arr) {
        KABillItem *billItem = [KABillItem billItemWithDict:dict];
        [marr addObject:billItem];
    }
    return marr;
}

-(void)writeDataToPlist:(NSString *)path data:(NSDictionary *)dict
{
    NSMutableArray *marr = [[NSMutableArray alloc]initWithContentsOfFile:path];
    [marr addObject:dict];
    [self writeToFile:path withData:marr];
}

-(NSMutableArray *)readMonthPlistFile
{
    NSMutableArray *colorArr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MonthColor.plist" ofType:nil];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    for (NSString *str in arr) {
        [colorArr addObject:[UIColor colorWithHexString:str]];
    }
    return colorArr;
}

@end
